package com.gem.talkbase.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.tsz.afinal.FinalBitmap;

import com.gem.talkbase.R;
import com.gem.talkbase.client.Client;
import com.gem.talkbase.util.GameView1;
import com.gem.talkbase.util.Split_String;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewManager;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;  
import android.widget.Toast;

public class Fragment_setting_try extends Activity {

	Button btn_start;
	GameView1 view,view1,view2;
	private static final int SENSOR_SHAKE = 0x283723;
	private SensorManager sensorManager;  
	
	int width, height;//屏幕宽和高
	List<Point[]> points;//骰子起始位置
	Context context;
	int count = 0;
//    private Vibrator vibrator; 
	
	private SoundPool sp;//声明一个SoundPool
	private int music,location;//定义一个整型用load（）；来设置suondID
    private Handler hand;
    private AnimationSet animationSet;
    
    
    //==============================================
    private FinalBitmap fb;//  加载图片
    private String imageurl = Client.Image_URL;

    private static final String[] m={"3","4","5","6",
    	                             "7","8","9","10","11","12",
    	                             "13","14","15","16","17","18"};
    private Spinner spinner;
    private ArrayAdapter<String> adapter;
    private  String strnum;
    
    
    private  LinearLayout zhu_try,zhu_try_middle;
    private  ImageView  imageview_try;
    private TextView my_num,ta_num,guli;
    private int sssnum = 0;
    
    private String[] getallid;
    private int    idnum;
    private String tochat;
  //==============================================
    
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.fragmrnt_setting_try);
		context = this;
		animationSet = new AnimationSet(true);
		
		//==============================================================
		zhu_try = (LinearLayout) this.findViewById(R.id.zhu_try);
		zhu_try.setVisibility(View.GONE);	//VISIBLE GONE	
		imageview_try = (ImageView) this.findViewById(R.id.image_try);
		 fb = FinalBitmap.create(this);//初始化FinalBitmap模块
		my_num = (TextView) this.findViewById(R.id.my_num);
		ta_num = (TextView) this.findViewById(R.id.ta_num);
		guli = (TextView) this.findViewById(R.id.guli);
		zhu_try_middle = (LinearLayout) this.findViewById(R.id.zhu_try_middle);
		
		Bundle extras = getIntent().getExtras();
		String hh = extras.getString("getallid");//获得从主页面传来的昵称
		
		if(hh!=null&&hh!=""&&hh.length()>4){
		 getallid = hh.split(Split_String.split_s1);
		 idnum =getallid.length;
		}
		
		//==============================================================
		
/*		zhu_try.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(tochat!=null&&tochat!=""){
				
				Intent intent = new Intent(Fragment_setting_try.this,Fragment_find_AddFriend.class);
				intent.putExtra("usernameid",tochat);		
				Fragment_setting_try.this.startActivity(intent);
				}
				
			}
		});*/
		
		

		hand = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				
				try{
				Integer[] num = new Integer[]{view.indexs[0]+1,view1.indexs[0]+1,view2.indexs[0]+1};
				Arrays.sort(num);
				int str = num[0]+num[1]+num[2];//num[0]+"+"+num[1]+"+"+num[2]+"="+   ====我摇的点数
				//=====================================
				String nummy = strnum.toString();
				
				int numta = sjs_function();    //  =======好友的点数
				
				int num1=0;
				try{
					num1=Integer.parseInt(nummy.toString().trim());    //======我选的点数
				} catch(NumberFormatException e){
					num1=-1;
				    //TODO
				    //添加异常处理
				}
				
				my_num.setText("你摇了"+str+"点");
				if(numta==sssnum){
					
				}else{
					ta_num.setText(numta+"");
					String picString = getallid[Integer.valueOf((int) (Math.random()*idnum))];			
					fb.display(imageview_try,imageurl+picString);
					tochat = picString.substring(0, picString.length()-4);
					System.out.println("==================fragmnet_try==================="+tochat);
					
					
				
				if(str==numta){
					sssnum = numta;
					guli.setText("你们平局哦！");
				}else if(str>numta){
					sssnum = numta;
					guli.setText("亲真厉害!");
				}else if(str<numta){
					sssnum = numta;
					guli.setText("亲,继续加油哦！");
					
				}
				
				}	
				
				
				
				/*if(str==num1){//=========================
				    Toast.makeText(context, "是"+str+",亲！你真厉害。", 0).show();
				}else if(str>num1){
					Toast.makeText(context, "是"+str+",亲！猜小啦！继续努力哦！", 0).show();
				}else if(str<num1){
					Toast.makeText(context, "是"+str+",亲！猜大啦！继续努力哦！", 0).show();
				}*/
			
				//Toast.makeText(MainActivity.this,strnum, 1).show();
				//=====================================
				
				
				 TranslateAnimation hideAciton = new TranslateAnimation(
						 0,100,0,100);
				 hideAciton.setDuration(500);
				 
				 Animation animation = new ScaleAnimation(1, 0, 1, 0);
				 animation.setDuration(500);
				 animation.setFillAfter(true);
				 
				 animationSet.addAnimation(animation);
				 animationSet.addAnimation(hideAciton);
				
				 animationSet.setFillAfter(true);
				 
				view.startAnimation(animationSet);
				view1.startAnimation(animationSet);
				view2.startAnimation(animationSet);
				sp.play(location, 1, 1, 0, 0, 1);
//				final ViewManager mViewManager = (ViewManager) getParent();
//				hand.postDelayed(new Runnable() {
//					
//					@Override
//					public void run() {
//						// TODO Auto-generated method stub
//						mViewManager.removeView(view);//移除view
//						mViewManager.removeView(view1);//移除view
//						mViewManager.removeView(view2);//移除view
//					}
//				}, 1000);
				} catch (NullPointerException e) {
					// TODO: handle exception
					e.printStackTrace();
					Toast.makeText(context, "亲！速度放慢点啦！", 0).show();
				}
				
				
				
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				zhu_try.setVisibility(View.VISIBLE);//=============================
				
				
				
				
			}
		};
		sp= new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);//第一个参数为同时播放数据流的最大个数，第二数据流类型，第三为声音质量
	    music = sp.load(this, R.raw.rotate, 1); //把你的声音素材放到res/raw里，第2个参数即为资源文件，第3个为音乐的优先级
	    location = sp.load(context, R.raw.location, 1);
		btn_start = (Button) this.findViewById(R.id.btn_start);
		
		//====================================================================
		

		spinner = (Spinner) findViewById(R.id.Spinner01);
		
		        //将可选内容与ArrayAdapter连接起来
		
		        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
		
		         
		
		        //设置下拉列表的风格
		
		        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		         
		
		        //将adapter 添加到spinner中
		
		        spinner.setAdapter(adapter);
		
		         
		
		        //添加事件Spinner事件监听 
		
		        spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
		
		         

		        //设置默认值
		
		        spinner.setVisibility(View.VISIBLE);
//=============================================================
		
		
		
		
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);  
//        vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);  
		
		DisplayMetrics dm = new DisplayMetrics();

		getWindowManager().getDefaultDisplay().getMetrics(dm);

		width = dm.widthPixels;//屏幕宽度

		height = dm.heightPixels;//屏幕高度
		points = new ArrayList<Point[]>();
		points.add(new Point[]{new Point(width/5, 5*height/24)});
		points.add(new Point[]{new Point(6*width/12, 5*height/24)});
		points.add(new Point[]{new Point(8*width/24, 9*height/24)});
//		points = new Point[]{new Point[]{new Point(width/5, 5*height/24)},new Point[]{new Point(8*width/24, 9*height/24)},new Point[]{new Point(6*width/12, 5*height/24)}};
		
//		view = new GameView(context, points.get(0),width,height,sp,0);
//		view1 = new GameView(context, points.get(1),width,height,sp,1);
//		view2 = new GameView(context, points.get(2),width,height,sp,2);
		btn_start.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				zhu_try.setVisibility(View.GONE);//===================================
				zhu_try_middle.setVisibility(View.GONE);//===================================
				// TODO Auto-generated method stub
//				 Animation animation = AnimationUtils.loadAnimation(context, R.anim.alpha_scale_rotate);
//				 animation.setFillAfter(true);
//				 btn_start.setAnimation(animation);
//				 btn_start.startAnimation(animation);
				if(count == 0 ){
					count++;
							sp.play(music, 1, 1, 0, 0, 1);
							view = new GameView1(context, points.get(0),width,height,sp,0,hand);
							view1 = new GameView1(context, points.get(1),width,height,sp,1,hand);
							view2 = new GameView1(context, points.get(2),width,height,sp,2,hand);
						addContentView(view, new LayoutParams(-2, -2));
						addContentView(view1, new LayoutParams(-2, -2));
						addContentView(view2, new LayoutParams(-2, -2));
						view.startThread();
						view1.startThread();
						view2.startThread();
					
				}else{
					Log.e("连续点击", (view != null && !view.flag) +"");
					if(view != null && !view.flag){//如果上次的线程没有跑了才允许开启此次线程
						sp.play(music, 1, 1, 0, 0, 1);
						view = new GameView1(context, points.get(0),width,height,sp,0,hand);
						view1 = new GameView1(context, points.get(1),width,height,sp,1,hand);
						view2 = new GameView1(context, points.get(2),width,height,sp,2,hand);
					addContentView(view, new LayoutParams(-2, -2));
					addContentView(view1, new LayoutParams(-2, -2));
					addContentView(view2, new LayoutParams(-2, -2));
					view.startThread();
					view1.startThread();
					view2.startThread();
					}
				}
			}
		});
		
		
	}

//	//线程来提醒UI线程不断重绘
//	public class GameThread extends Thread{
//		public void run(){
//			while (view.flag){
//				try {
//					Thread.sleep(100);
//					view.postInvalidate();
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//			}
//		}
//	}
	
	//传感器监听
	private SensorEventListener sensorEventListener = new SensorEventListener() {

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			
		}

		// 传感器信息改变时执行该方法  
		@Override
		public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;  
            float x = values[0]; // x轴方向的重力加速度，向右为正  
            float y = values[1]; // y轴方向的重力加速度，向前为正  
            float z = values[2]; // z轴方向的重力加速度，向上为正  
            int medumValue = 16; 
            if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) {  
//                vibrator.vibrate(200);  
                Message msg = new Message();  
                msg.what = SENSOR_SHAKE;  
                handler.sendMessage(msg);  
            }  
		}
		
	};
	
	Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			count ++;
			zhu_try.setVisibility(View.GONE);//=======================================
			zhu_try_middle.setVisibility(View.GONE);//===================================
			if(msg.what == SENSOR_SHAKE){
				if(count == 1 ){//第一次摇
					sp.play(music, 1, 1, 0, 0, 1);
					view = new GameView1(context, points.get(0),width,height,sp,0,hand);
					view1 = new GameView1(context, points.get(1),width,height,sp,1,hand);
					view2 = new GameView1(context, points.get(2),width,height,sp,2,hand);
				addContentView(view, new LayoutParams(-2, -2));
				addContentView(view1, new LayoutParams(-2, -2));
				addContentView(view2, new LayoutParams(-2, -2));
//				new GameThread().start();
				view.startThread();
				view1.startThread();
				view2.startThread();
//					}
				}else{
					if(view != null && !view.flag){//如果上次的线程没有跑了才允许开启此次线程
						sp.play(music, 1, 1, 0, 0, 1);
						view = new GameView1(context, points.get(0),width,height,sp,0,hand);
						view1 = new GameView1(context, points.get(1),width,height,sp,1,hand);
						view2 = new GameView1(context, points.get(2),width,height,sp,2,hand);
					addContentView(view, new LayoutParams(-2, -2));
					addContentView(view1, new LayoutParams(-2, -2));
					addContentView(view2, new LayoutParams(-2, -2));
//					new GameThread().start();
					view.startThread();
					view1.startThread();
					view2.startThread();
					}
					
				}
			}
		}
	} ;
	
	// 注册监听器 
	@Override  
    protected void onResume() {  
        super.onResume();  
        if (sensorManager != null) { 
            sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);  
            // 第一个参数是Listener，第二个参数是所得传感器类型，第三个参数值获取传感器信息的频率  
        }  
    }  
	
	// 取消监听器 
	 @Override  
	 protected void onPause() {  
	    super.onPause();  
	    if (sensorManager != null) { 
	        sensorManager.unregisterListener(sensorEventListener);  
	    }  
	 } 
	 
	 
	 
	 
	 
	//使用数组形式操作--得到spnner的值
	
	     class SpinnerSelectedListener implements OnItemSelectedListener{
	 
	  
	 
	         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
	
	                 long arg3) {
	
	            // view.setText("你的血型是："+m[arg2]);
	        	 strnum = m[arg2];
	        	 
	 
	         }
	
	  
	
	         public void onNothingSelected(AdapterView<?> arg0) {
	 
	         }
	
	     }

	  //获得随机数
	     public int  sjs_function(){
	 		int a1,a2,a3;
	 		a1=Integer.valueOf((int) (Math.random()*6));
	 		a2=Integer.valueOf((int) (Math.random()*6));
	 		a3=Integer.valueOf((int) (Math.random()*6));
	 		return a1+a2+a3;
	
	 	} 
	 
	 
}
